package com.kkd.kkdapp.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.ViewPagerAadapter;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.utils.PreferenceUtils;

/**
 * 欢迎界面
 * 
 * @author mty
 */
public class WelcomeActivity extends BaseActivity implements
		OnPageChangeListener {

	private ViewPager welcome_vvp;
	private int[] imaIdArray = { R.drawable.welcome_icon_1,
			R.drawable.welcome_icon_3, R.drawable.welcome_icon_4 }; // 图片数据源
	private ArrayList<ImageView> dots; // 小点图片
	private ArrayList<RelativeLayout> tips; // 图片
	private ViewGroup group; // 小点组
	private ImageView imageView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_welcome);
		mTintManager.setTintColor(0x00000000);
		PreferenceUtils.getInstance().welcomeBoolean(this, true);
		initView();
		setListener();
		titleViewPager();
	}

	/**
	 * 初始化组件
	 */
	public void initView() {
		welcome_vvp = (ViewPager) this.findViewById(R.id.welcome_vvp);
		group = (ViewGroup) this.findViewById(R.id.welcome_ll);

	}

	/**
	 * 注册监听
	 */
	public void setListener() {
	}

	// 设置ViewPager
	public void titleViewPager() {

		// 滑动点
		dots = new ArrayList<ImageView>();
		for (int i = 0; i < imaIdArray.length; i++) {
			ImageView imageView = new ImageView(this);
			imageView.setLayoutParams(new LayoutParams(10, 10));
			dots.add(imageView);
			if (i == 0) {
				dots.get(i).setBackgroundResource(R.drawable.page_focused1);
			} else {
				dots.get(i).setBackgroundResource(R.drawable.page_unfocused1);
			}

			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
					new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
							LayoutParams.WRAP_CONTENT));
			layoutParams.leftMargin = 10;
			layoutParams.rightMargin = 10;
			group.addView(imageView, layoutParams);
		}

		// 将图片装载到数组中
		tips = new ArrayList<RelativeLayout>();
		for (int i = 0; i < imaIdArray.length; i++) {
			RelativeLayout layout = new RelativeLayout(this);
			layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
					LayoutParams.FILL_PARENT));

			imageView = new ImageView(this);
			imageView.setLayoutParams(new LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
			imageView.setScaleType(ImageView.ScaleType.FIT_XY);
			imageView.setBackgroundResource(imaIdArray[i]);
			layout.addView(imageView);
			tips.add(layout);
		}
		ViewPagerAadapter dapter = new ViewPagerAadapter(tips);
		// 设置Adapter
		welcome_vvp.setAdapter(dapter);
		welcome_vvp.setOnPageChangeListener(this);
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	@Override
	public void onPageSelected(int arg0) {
		if (arg0 == 2) {
			imageView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					gotoHomePage();

				}
			});
		}
		// if(arg0==3){
		// group.setVisibility(View.GONE);
		// }else{
		// group.setVisibility(View.VISIBLE);
		// }
		setImageBackground(arg0);
	}

	private void setImageBackground(int selectItems) {
		selectItems = selectItems % dots.size();
		for (int i = 0; i < dots.size(); i++) {
			if (i == selectItems) {
				dots.get(i).setBackgroundResource(R.drawable.page_focused1);
			} else {
				dots.get(i).setBackgroundResource(R.drawable.page_unfocused1);
			}
		}
	}

	private void gotoHomePage() {
		Intent intent = new Intent(this, HomeActivity.class);
		startActivity(intent);
		finish();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {

		}
		return super.onKeyDown(keyCode, event);
	}

}
